var GameManager = Class.extend({
	canvas: null,
	ctx: null,
	entities: [],
	inputManager: null,
	vetBalls: null,
	ballPos: [
		{
			x: 580,
			y: 36
		},
		{
			x: 580,
			y: 100
		},
		{
			x: 580,
			y: 292
		},
		{
			x: 580,
			y: 164
		},
		{
			x: 32,
			y: 100
		},
		{
			x: 32,
			y: 164
		},
		{
			x: 32,
			y: 320
		},
		{
			x: 32,
			y: 420
		}
	],
	
	initBalls: function (){
		this.vetBalls = [];
		for (var p=0; p<this.ballPos.length; p++) {
			var bola = new Bola();
			bola.pos = this.ballPos[p];
			this.vetBalls.push(bola);
		}
		vetBalls = this.vetBalls;
	},


	init: function(canvas) {
		this.canvas = canvas;
		this.ctx = canvas.getContext("2d");
		this.inputManager = new InputManager(canvas);
		this.initBalls();
	},
	
	setBG: function (imgSrc) {
		this.canvas.style.backgroundImage = 'url("'+imgSrc+'")';	
	},

	addEntity: function(entity) {
		this.entities.push(entity);
	},

	update: function() {
		var fuleco, toColideEntities = [];
		for (var i = 0; i < this.entities.length; i++) {
			if (this.entities[i] == null) {
				continue;
			}
			this.entities[i].update();
			if (this.isColisionEntity(this.entities[i].type)) {
				toColideEntities.push(this.entities[i]);
			};
			if (this.entities[i].type == 'mascote') {
				fuleco = this.entities[i];
			};
		};
		this.checkColision(fuleco, toColideEntities);
	},

	isColisionEntity: function (type) {
		var toColide = false;
		switch(type){
			case 'bola':
			case 'juiz':
			case 'estadio':
				toColide = true;
		}
		return toColide;
	},

	checkColision: function (fuleco, entities) {
		var posX1 = fuleco.pos.x,
			posX2 = posX1 + fuleco.tamanho.w,
			posY1 = fuleco.pos.y,
			posY2 = posY1 + fuleco.tamanho.h,
			colided = false, entity;
		for (var e=0; e<entities.length; e++) {
			var entityPosX1 = entities[e].pos.x,
				entityPosX2 = entityPosX1 + entities[e].tamanho.w,
				entityPosY1 = entities[e].pos.y,
				entityPosY2 = entityPosY1 + entities[e].tamanho.h,
				colision = false;
			if ((posX1 >= entityPosX1 && posX1 <= entityPosX2) && ((posY1 >= entityPosY1 && posY1 <= entityPosY2) || (posY2 >= entityPosY1 && posY2 <= entityPosY2))) {
				colision = true;
			}
			if ((posX2 >= entityPosX1 && posX2 <= entityPosX2) && ((posY1 >= entityPosY1 && posY1 <= entityPosY2) || (posY2 >= entityPosY1 && posY2 <= entityPosY2))) {
				colision = true;
			}
			if ((posY1 >= entityPosY1 && posY1 <= entityPosY2) && ((posX1 >= entityPosX1 && posX1 <= entityPosX2) || (posX2 >= entityPosX1 && posX2 <= entityPosX2))) {
				colision = true;
			}
			if ((posY2 >= entityPosY1 && posY2 <= entityPosY1) && ((posX1 >= entityPosX1 && posX1 <= entityPosX2) || (posX2 >= entityPosX1 && posX2 <= entityPosX2))) {
				colision = true;
			}
			if (colision === true) {
				fuleco.collided(entities[e]);
				break;
			}
		};
	},

	draw: function() {
		this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
		for (var i = 0; i < this.entities.length; i++) {
			var entity = this.entities[i];
			if (entity == null || entity.removed === true) {
				this.entities[i] = null;
				continue;
			}
			ctx.drawImage(entity.image, entity.pos.x, entity.pos.y);
		}
	},
	emptyEntities: function(){
		this.entities = [];
		this.inputManager.keyState = [];
		this.inputManager.direction = null;
	}
});